<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>

<html>
<head>
<title>通过JSP打开数据表</title>
</head>
<body>

 <%

          Class.forName("com.mysql.jdbc.Driver");   ////驱动程序名
          String url = "jdbc:mysql://192.168.10.50:3306/class";   //数据库名
            String username = "natasha";     //数据库用户名
            String password = "123456";  //数据库用户密码
            Connection conn = DriverManager.getConnection(url, username, password);  //连接状态

            if(conn != null){  
                out.print("数据库连接成功！"); 
                out.print("<br />");            
                Statement stmt = null;
                ResultSet rs = null;  
                String sql = "SELECT * FROM m_class;";  //查询语句
                stmt = conn.createStatement();  
                rs = stmt.executeQuery(sql); 
                out.print("查询结果：");  
                out.print("<br />");  
                out.println("kid" + "  " + "课程名称 ");  
                out.print("<br />");  
                while (rs.next()) {  
                 out.println(rs.getInt("kid")+ " &nbsp " + rs.getString("课程名称")); //将查询结果输出  
                 out.print("<br />"); 
                }  
            }
            else{  
                out.print("连接失败！");  
            }

%>   
</body>
</html>
